package ru.spb.osll.myfit.client;

import ru.spb.osll.myfit.client.event.PageButtonEvent;
import ru.spb.osll.myfit.client.event.PageEvent;
import ru.spb.osll.myfit.client.event.PageButtonEvent.PageButtonEventType;
import ru.spb.osll.myfit.client.event.PageEvent.PageEventType;
import ru.spb.osll.myfit.client.manager.PageExecutingManager;
import ru.spb.osll.myfit.client.widget.PageButton;
import ru.spb.osll.myfit.client.widget.PageButtonEventListener;
import ru.spb.osll.myfit.client.widget.PageWidget;
import ru.spb.osll.myfit.client.widget.ProgressBar;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageExecutingResult.PageExecutingState;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * 
 *
 */
public class MyFitView {

  private VerticalPanel m_rootPanel;

  private VerticalPanel m_editPanel;

  private Panel m_viewModePanel;

  private Panel m_editModePanel;

  private Widget m_contentPanel;

  private String m_pagePath;

  private PageMode mode;

  private final Anchor anch;

  private PageWidget m_pageWidget;

  private PageExecutingManager m_executingManager;

  private PageButtonEventListener m_buttonEventListener;

  private Fitnesse m_fitnesse;

  public MyFitView(String pagePath, PageWidget pageWidget, PageExecutingManager executingManager, Fitnesse fitnesse) {
    m_buttonEventListener = createButtonEventListener();
    m_executingManager = executingManager;
    m_fitnesse = fitnesse;
    m_pagePath = pagePath;
    m_pageWidget = pageWidget;
    mode = PageMode.VIEW;
    anch = new Anchor(getTitleName());
    pageWidget.setMode(mode);
    createRootPanel();

  }

  private PageButtonEventListener createButtonEventListener() {
    PageButtonEventListener listener = new PageButtonEventListener() {

      @Override
      public void actionPerformed(PageButtonEvent event) {
        switch (event.getType()) {
        case ADD_TABLE:
          m_pageWidget.actionPerformed(new PageEvent(PageEventType.ADD_TABLE));
          break;
        case ADD_INLUDE:
          m_pageWidget.actionPerformed(new PageEvent(PageEventType.ADD_INCLUDE));
          break;
        case ADD_WIKI:
          m_pageWidget.actionPerformed(new PageEvent(PageEventType.ADD_WIKI));
          break;
        case COLLAPSE_ALL:
          m_pageWidget.setOpen(false);
          break;
        case EXPAND_ALL:
          m_pageWidget.setOpen(true);
          break;
        case SAVE_PAGE:
          m_fitnesse.savePage(m_pagePath, m_pageWidget.getPage());
          break;
        case TEST_PAGE:
          m_executingManager.executeTestPage(m_pageWidget, m_pagePath);

          break;
        case REFRESH:
          com.google.gwt.user.client.Window.Location.reload();
          break;

        default:
          break;
        }
      }
    };
    return listener;
  }

  private void createRootPanel() {
    m_rootPanel = new VerticalPanel();
    m_rootPanel.setStyleName("root-panel");

    Label myFitLabel = new Label();
    myFitLabel.setStyleName("main-label");
    m_rootPanel.add(myFitLabel);

    createEditPanel();
    createContentPanel();

    m_rootPanel.add(m_editPanel);
    m_rootPanel.add(m_contentPanel);
  }

  private void createEditPanel() {
    m_editPanel = new VerticalPanel();
    m_editPanel.setSpacing(6);
    m_editPanel.setStyleName("editPanelTitle");

    anch.setStyleName("editTitleText");
    anch.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        if (mode.equals(PageMode.EDIT)) {
          mode = PageMode.VIEW;
          m_editPanel.remove(m_editModePanel);
          m_editPanel.add(m_viewModePanel);
        } else if(!m_executingManager.testStarted()){
			mode = PageMode.EDIT;
			m_editPanel.remove(m_viewModePanel);
			m_editPanel.add(m_editModePanel);
        }
        anch.setText(getTitleName());
        m_pageWidget.setMode(mode);
      }
    });
    m_editPanel.add(anch);

    createEditModePanel();
    createViewModePanel();
    if (mode.equals(PageMode.EDIT)) {
      m_editPanel.add(m_editModePanel);
    } else {
      m_editPanel.add(m_viewModePanel);
    }
  }

  private void createEditModePanel() {
    m_editModePanel = new VerticalPanel();
    ((VerticalPanel) m_editModePanel).setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    HorizontalPanel hPanel = new HorizontalPanel();
    hPanel.setSpacing(5);
    hPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    hPanel.add(new PageButton("Save page", "sendButtonStyle", PageButtonEventType.SAVE_PAGE, m_buttonEventListener));
    HorizontalPanel separator = new HorizontalPanel();
    separator.setWidth("20");
    hPanel.add(separator);
    hPanel.add(new PageButton("Discard", "discardButtonStyle", PageButtonEventType.REFRESH, m_buttonEventListener));
    hPanel
        .add(new PageButton("Add table", "addTableButtonStyle", PageButtonEventType.ADD_TABLE, m_buttonEventListener));
    hPanel.add(new PageButton("Include page", "importPageButtonStyle", PageButtonEventType.ADD_INLUDE,
        m_buttonEventListener));
    hPanel.add(new PageButton("Expand all", "expandAllIncludesButtonStyle", PageButtonEventType.EXPAND_ALL,
        m_buttonEventListener));
    hPanel.add(new PageButton("Collapse all", "collapseAllIncludesButtonStyle", PageButtonEventType.COLLAPSE_ALL,
        m_buttonEventListener));
    hPanel
        .add(new PageButton("Input text", "textInputButtonStyle", PageButtonEventType.ADD_WIKI, m_buttonEventListener));
    m_editModePanel.add(hPanel);
  }

  private void createViewModePanel() {
    m_viewModePanel = new HorizontalPanel();
    m_viewModePanel.setWidth("100%");

    HorizontalPanel hPanel = new HorizontalPanel();
    hPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    hPanel.setSpacing(5);

    hPanel.add(new PageButton("Test page", "testButtonStyle", PageButtonEventType.TEST_PAGE, m_buttonEventListener));
    HorizontalPanel separator = new HorizontalPanel();
    separator.setWidth("20");
    hPanel.add(separator);
    hPanel.add(new PageButton("Expand all", "expandAllIncludesButtonStyle", PageButtonEventType.EXPAND_ALL,
        m_buttonEventListener));
    hPanel.add(new PageButton("Collapse all", "collapseAllIncludesButtonStyle", PageButtonEventType.COLLAPSE_ALL,
        m_buttonEventListener));

    HorizontalPanel right = new HorizontalPanel();
    right.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    ProgressBar pBar = new ProgressBar();
    m_executingManager.setProgressBar(pBar);
    right.setWidth("100%");
    right.add(pBar);

    m_viewModePanel.add(hPanel);
    m_viewModePanel.add(right);
  }

  private void createContentPanel() {
    m_contentPanel = m_pageWidget;
  }

  public Widget getRootWidget() {
    return m_rootPanel;
  }

  public void setPagePath(String pagePath) {
    m_pagePath = pagePath;
    m_pageWidget.setPagePath(m_pagePath);
    anch.setText(getTitleName());
  }

  private String getTitleName() {
    String title;
    String path = "";
    if (!m_pagePath.isEmpty()) {
      path = "\"" + m_pagePath + "\"";
    }

    if(mode == PageMode.VIEW){
      title = PageMode.EDIT.toString() + "   " + path;
    }else{
      title = PageMode.VIEW.toString() + "   " + path;
    }
    return title;
  }

  public void clear() {
    m_pageWidget.clear();
  }

  public void addPageElementWidget(PageElement element) {
    m_pageWidget.addPageElementWidget(element);
  }
}
